package com.ly.controller;

import com.ly.bean.Admin;
import com.ly.bean.EasybuyProductCategory;
import com.ly.bean.MonthCost;
import com.ly.bean.Result;
import com.ly.service.AdminService;
import com.ly.service.ProductCategoryService;
import com.ly.util.CookieUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 李岩
 * @date 2021/12/27-9:43
 * @describe
 */
@Controller
@RequestMapping("/admin/")
public class AdminController {
    private final Logger logger= LoggerFactory.getLogger(AdminController.class);

    @Reference(url = "dubbo://localhost:20886")
    private AdminService adminService;

    @Reference(url = "dubbo://localhost:20851")
    private ProductCategoryService productCategoryService;

    private AdminController(){
        System.out.println("进入了AdminController  controller层");
    }

    @RequestMapping("adminLogin")
    public String adminLogin(HttpServletRequest request, HttpServletResponse response){
        System.out.println("登录的用户为");
        try {
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            Admin loginAdmin = adminService.selectAdminByaName(name);
            if (loginAdmin==null){
                //Result.getResult(false,444,"用户名不存在",null);
                return "login";
            }
            logger.info("admin 登录成功  "+loginAdmin);
            //把登录用户id保存到cookie
            CookieUtil.setCookie(request,response,"adminId",loginAdmin.getAId()+"");
            //把登录用户信息保存到redis中
            adminService.saveLloginAdminToRedis(loginAdmin);
            //Result.getResult(true,200,"查询成功",loginAdmin);
            return "redirect:/main/showMain";
        } catch (Exception e) {
            logger.error("admin 登录出现异常  "+e);
        }
        //Result.getResult(false,444,"查询失败",null);
        return "error";
    }
    @RequestMapping("logout")
    public String logout(@CookieValue(required = false) String adminId,HttpServletRequest request,HttpServletResponse response){
        try {
            logger.info("管理员退出登录 ");
            if (adminId==null || "".equals(adminId))
                return "login";
            //从redis中删除登录的用户信息
            adminService.deleteLoginAdminFromRedis(Integer.valueOf(adminId));
            //从cookie中删除登录用户的信息
            CookieUtil.deleteCookie(request,response,"adminId");
            return "login";
        } catch (Exception e) {
            logger.error("管理员退出登录 出现异常 "+e);
        }
        return "error";
    }
    //各种销售商品分类统计的饼状图实现
    @RequestMapping("selectSellNameAndValue")
    @ResponseBody
    public Result selectSellNameAndValue(){
        try {
            logger.info("各种销售商品分类统计的饼状图实现");
            List<EasybuyProductCategory> list = productCategoryService.selectSellNameAndValue();
            for (EasybuyProductCategory category : list) {
                System.out.println("category "+category);
            }
            return Result.getResult(true,200,"查询成功",list);
        } catch (Exception e) {
            logger.error("各种销售商品分类统计的饼状图实现 出现异常 "+e);
        }
        return Result.getResult(false,444,"查询失败",null);
    }

    //月销售额的折线图实现
    @RequestMapping("selectMonthCost")
    @ResponseBody
    public Result selectMonthCost(){
        try {
            logger.info("月销售额的折线图实现");
            List<MonthCost> list = productCategoryService.selectMonthCost("2021");
            for (MonthCost monthCost : list) {
                System.out.println("monthCost "+monthCost);
            }
            return Result.getResult(true,200,"查询成功",list);
        } catch (Exception e) {
            logger.error("月销售额的折线图实现 出现异常 "+e);
        }
        return Result.getResult(false,444,"查询失败",null);
    }
}
